<?php
namespace Com\Pay;
class DafuWengPay{

    private $config = array();

    public function __construct($mchConfig,$config = null){
        $this->config = $mchConfig;
    }

    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/DafuWengPayNotify';
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            "uid" => $this->config['mch_id'],
            "orderNo" => $order_sn,
            "mchName" => 'Cbpay',
            "price" => $price,
            "backUrl"   => $order_info['return_url'],
            "postUrl" => $notifyUrl,
            "payType" => $order_info['payType'],
        );
        $sign = $this->setSign($post_data,$this->config['mch_key'],false);
        $post_data['sign'] = $sign;
        $tar_url = 'http://api.le6ss.cn/api/precreatetrade';
        $res = json_decode(https_request($tar_url,$post_data));
        if($res->resultCode == 'success'){
            return $res->payUrl;
        }
        return false;
    }

    // 生成签名方法
    private function setSign($param,$key,$IS_Notify){
        $str = "";
        ksort($param);
        foreach($param as $key1=>$val)  {
            if($IS_Notify && $key1 == 'sign') continue;
            $str.=$key1."=".$val."&";
        }
        $str = rtrim($str,'&');
        $sign = strtoupper(md5($str . '&key=' . $key));
        return $sign;
    }


    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $Mysign = $this->setSign($data,$this->config['mch_key'],true);
        if($Mysign === $data['sign']){
            if($data['resultCode'] == 'success'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }
}
